/**
 * Created by yangxianming on 2019/10/22.
 */
//
//接口定义 都是以api开头 所需参数都通过方法传入
//
//拿到设计稿列表
function apiGetResult(dofun, fileName){
    var data = {
        "action":"getResult",
        "fileName":fileName
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                    apiGetList(drawGetList);
                    //window.open("../data/uploadQRcode/"+fileName+"/result");
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/uploadQRcode/uploadQRcodeDispense", true);
    xmlHttp1.send(JSON.stringify(data));

    //var xmlHttp2 = new XMLHttpRequest();
    //xmlHttp2.open("GET", "../data/uploadQRcode/"+fileName+"/result.txt", true);
    //xmlHttp2.send();
}
function apiUnzip(fileName){
    var data = {
        "action":"unzip",
        "fileName":fileName
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    apiGetList(drawGetList);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/uploadQRcode/uploadQRcodeDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}
function apiGetList(dofun){
    var data = {
        "action":"getList"
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    dofun(r["result"]);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "/uploadQRcode/uploadQRcodeDispense", true);
    xmlHttp1.send(JSON.stringify(data));
}
//上传设计稿
function apiUploadFile(){
    var fi = document.getElementById('file').files[0];
    var fileName = document.getElementById('file').value;
    var upload = document.getElementById("upload");
    var action = "action=uploadQRcode";

    if(fi == undefined){
        alert("需要选择上传的设计稿");
        return;
    }
    var fileEnd = fileName.substring(fileName.length-3, fileName.length);
    if(fileEnd != "zip"){
        alert("只能zip格式");
        return;
    }

    //不要猛点
    upload.setAttribute("disabled", "");
    upload.style.color = "red";
    var form = new FormData();
    form.append("file", fi);

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "1"){
                    var full = fileName + r["result"];
                    apiUnzip(full);
                }
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
            }
        }
    };
    xmlHttp1.open("POST", "../uploadFile?" + action, true);
    xmlHttp1.send(form);

    //loadXMLDoc("post", "../uploadFile?" + action + "&projectName=" + projectValue + "&version=" + version, form, null);
    controlTopDiv('upLoadFile', 'close');
    setTimeout(
        function(){
            upload.removeAttribute("disabled");
            upload.style.color = "black";
        }, 5000);
}

//
//页面渲染 都是以draw开头
//
//列出结果文件列表
function drawGetResult(data){
    var resultList = document.getElementById("resultList");
    resultList.innerText = "";
    for(var i in data){
        var d = document.createElement("div");
        var br = document.createElement("br");
        d.innerText = data[i];
        d.appendChild(br);
        resultList.appendChild(d);
    }

}

//列出列表
function drawGetList(data){
    var tbody = document.getElementById("uploadFileList").getElementsByTagName("tbody")[0];
    var tr0 = tbody.getElementsByTagName("tr")[0];
    tbody.innerText = "";
    tbody.append(tr0);
    for(var i = 0; i < data.length; i++){
        var tr = document.createElement("tr");
        var td_fileName = document.createElement("td");
        var td_time = document.createElement("td");
        var td_burn = document.createElement("td");
        tr.setAttribute("fullName", data[i]["fullName"]);
        td_fileName.innerText = data[i]["fileName"];
        td_time.innerText = data[i]["time"];
        if(data[i]["burn"]){
            td_burn.innerText = "已焚";
        }else{
            if(data[i]["resultFile"]){
                td_burn.style.cursor = "pointer";
                td_burn.onclick = function(){readResultFile(this.parentNode.getAttribute("fullName"))};
                td_burn.innerText = "阅后即焚";
            }else{
                td_burn.innerText = "生成中...";
            }
        }
        tr.appendChild(td_fileName);
        tr.appendChild(td_time);
        tr.appendChild(td_burn);
        tbody.appendChild(tr);

    }
}

//
//页面调用 数据获取，接口层与渲染层的连接
//
function initializeData(){
    apiGetList(drawGetList);
}

//阅读上传结果文件
function readResultFile(fullName){
    apiGetResult(drawGetResult, fullName);
}

//弹出层控制
function controlTopDiv(whichWindow, mySwitch, ob){
    var topDiv = document.getElementById(whichWindow);
    if (mySwitch == "open") {
        var left = ob.offsetLeft;
        var top = ob.offsetTop + ob.offsetHeight + 10;
        //显示页面
        topDiv.style.marginTop = document.documentElement.scrollTop + "px";
        topDiv.style.display = "block";
        //设置位置
        var topDiv1001 = topDiv.getElementsByClassName("topDiv1001")[0];
        topDiv1001.style.marginLeft = left + "px";
        topDiv1001.style.marginTop = top + "px";
    }

    if (mySwitch == "close") topDiv.style.display = "none";
}




function projectSelectOnclick(){

}